package com.qffx.moviebase.adapter.classify;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qffx.moviebase.R;
import com.qffx.moviebase.adapter.ArticleAdapter;
import com.qffx.moviebase.adapter.AuthorListAdapter;
import com.qffx.moviebase.bean.classify.ArticleEntity;
import com.qffx.moviebase.bean.classify.AuthorEntity;
import com.qffx.moviebase.bean.classify.ClassifyBase;
import com.qffx.moviebase.bean.classify.ClassifyEntity;
import com.qffx.moviebase.bean.classify.SpecialEntity;
import com.qffx.moviebase.view.classify.ClassifySpeacialMoreActivity;

import java.util.ArrayList;
import java.util.List;


public class  ClassifyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<ClassifyBase> list;
    private Context context;
    private LayoutInflater inflater;
    private int TYPE_SPECIAL = 1;
    private int TYPE_AUTHOR = 2;
    private int TYPE_ARTICLE = 3;
    private ILoaderMore loaderMore;

    public ClassifyAdapter(List<ClassifyBase> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_SPECIAL) {
            View view = inflater.inflate(R.layout.item_classify_speacial, parent, false);
            ViewHolder1 viewHolder1 = new ViewHolder1(view);
            return viewHolder1;

        } else if (viewType == TYPE_AUTHOR) {
            View view = inflater.inflate(R.layout.item_classify_authorlist, parent, false);
            return new ViewHolder2(view);
        } else if (viewType == TYPE_ARTICLE) {
            View view = inflater.inflate(R.layout.classify_article, parent, false);
            return new ViewHolder3(view);
        } else if (viewType == R.layout.loadmore) {
            View view = inflater.inflate(R.layout.loadmore, parent, false);
            return new LoadMoreViewHolder(view);
        }

        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if (type == TYPE_SPECIAL) {
            List<ClassifyEntity.TopicRecommendListBean> data = new ArrayList<>();
            data.addAll(((SpecialEntity) list.get(position)).getList());
            ClassifySpeacialAdapter adapter = new ClassifySpeacialAdapter(data, context);
            ((ViewHolder1) holder).recyclerView.setLayoutManager(new GridLayoutManager(context, 2, LinearLayoutManager.VERTICAL, false));
            ((ViewHolder1) holder).recyclerView.setAdapter(adapter);
            ((ViewHolder1) holder).tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    context.startActivity(new Intent(context, ClassifySpeacialMoreActivity.class));
                }
            });

        } else if (type == TYPE_AUTHOR) {
            List<ClassifyEntity.AuthorListBean> data = new ArrayList<>();
            data.addAll(((AuthorEntity) list.get(position)).getList());
            Log.d("info", "----------" + data.size());
            AuthorListAdapter adapter = new AuthorListAdapter(data, context);
            ((ViewHolder2) holder).recycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            ((ViewHolder2) holder).recycler.setAdapter(adapter);

        } else if (type == TYPE_ARTICLE) {
            List<ClassifyEntity.ArticleListBean> data = new ArrayList<>();
            data.addAll(((ArticleEntity) list.get(position)).getList());
            ArticleAdapter adapter = new ArticleAdapter(data, context);
            ((ViewHolder3) holder).recycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            ((ViewHolder3) holder).recycler.setAdapter(adapter);

        } else {
            loaderMore.loadMore();
        }

    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == list.size()) {
            return R.layout.loadmore;
        } else {
            ClassifyBase base = list.get(position);
            if (base instanceof SpecialEntity) {
                return TYPE_SPECIAL;
            } else if (base instanceof AuthorEntity) {
                return TYPE_AUTHOR;
            } else if (base instanceof ArticleEntity) {
                return TYPE_ARTICLE;
            }
        }
        return 0;
    }

    public static class ViewHolder1 extends RecyclerView.ViewHolder {

        private RecyclerView recyclerView;
        private TextView tv;

        public ViewHolder1(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.class_speacial_recycler);
            tv = (TextView) itemView.findViewById(R.id.speacial_more_tv);

        }
    }

    public static class ViewHolder2 extends RecyclerView.ViewHolder {

        private RecyclerView recycler;

        public ViewHolder2(View itemView) {
            super(itemView);
            recycler = (RecyclerView) itemView.findViewById(R.id.author_recycler);
        }
    }

    public static class ViewHolder3 extends RecyclerView.ViewHolder {

        private RecyclerView recycler;

        public ViewHolder3(View itemView) {
            super(itemView);
            recycler = (RecyclerView) itemView.findViewById(R.id.article_recycler);
        }
    }

    public static class LoadMoreViewHolder extends RecyclerView.ViewHolder {

        public LoadMoreViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface ILoaderMore {
        void loadMore();
    }

    public void setLoadListener(ILoaderMore loaderMore) {
        this.loaderMore = loaderMore;
    }

}
